<template>
	<!-- 用户收藏页面 -->
	<div style="width: 100%;">
		<Header></Header>

		<div style="width: 1200px;margin: auto;margin-top: 30px;">

			<el-table :data="sc" style="width: 100%">
				<el-table-column label="序号" width="100">
					<template #default="scope">
						{{scope.$index+1}}
					</template>
				</el-table-column>
				<el-table-column label="商品名" width="300" prop='goodsname'>
				</el-table-column>
				<el-table-column width="150">
					<template #default="scope">
						<img :src="scope.row.imgs" style="width: 120px;height:80px;" />
					</template>
				</el-table-column>
				<el-table-column label="时间" width="150">
					<template #default="scope">
						{{formatTime(scope.row.stime)}}
					</template>
				</el-table-column>
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button size="small" type='daner' @click="deleteSupplier(scope.row,scope.$index)">取消收藏
						</el-button>
						<el-button size="small" type='daner' @click="editSupplier(scope.row)">查看商品</el-button>
					</template>
				</el-table-column>
			</el-table>

		</div>
	</div>
</template>
<script>
	import Axios from 'axios'
	import {
		ElMessageBox,
		ElMessage
	} from 'element-plus'
	import router from "../route/index.js"
	import Header from './Header'
	export default {
		name: 'Shoucang',
		components: {
			Header
		},
		data() {
			return {
				sc: [],
				users: {},
				leixing: [],

			}
		},
		created() {
			this.getUsers();
			this.getSupplier();
			this.getType();
		},
		methods: {
			getType() {
				Axios.post('/shopmng/api/type/selectAll').then(res => {
					this.leixing = res.data;
				});
			},
			getUsers() {
				// 获取sessionstorage中的用户信息
				let str = sessionStorage.getItem('users')
				if (str != null) {
					//把字符串转换成对象 --- sessionStorage存的必须时字符串，所以取出来的也是字符串
					this.users = JSON.parse(str);
				} else {
					alert('请先登录')
				}
			},
			getSupplier() {
				Axios.post('/shopmng/api/shoucang/selectUid', {
					uid: this.users.uid
				}).then(res => {
					this.sc = res.data;
				});
				
			},
			formatTime(val) {
				return val.substr(0, 10)
			},
			deleteSupplier(obj, index) {
				ElMessageBox.confirm(
					'确认删除吗？', {
						confirmButtonText: '确认',
						cancelButtonText: '取消',
						type: 'warning',
					}
				).then(() => {
					Axios.post('/shopmng/api/shoucang/deleteOne', {
						'sid': obj.sid
					}).then(res => {
						if (res.data.affectedRows == 1) {
							this.sc.splice(index, 1);
							ElMessage({
								message: '删除成功',
								type: 'success',
							});
						} else {
							ElMessage({
								message: '删除失败',
							});
						}
					});

				})
			},

			editSupplier(val) {
				for(let i=0;i<this.leixing.length;i++){
					if(this.leixing[i].id==val.type){
						val.typename = this.leixing[i].typename;
					}
				}
				sessionStorage.setItem('g', JSON.stringify(val));
				router.push('/ShowOne');
			}
		}

	}
</script>

<style>
</style>